package com.xbs.dao.content;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 权限分配dao层
 */
import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.EmployeeEntity;
import com.xbs.entity.LimitEntity;
import com.xbs.entity.RoleEntity;
import com.xbs.entity.RoleLimitEntity;

public class LimitAdminDao extends BaseDao {
	//角色
	private RowDatas<RoleEntity> re;
	//权限
	private RowDatas<LimitEntity> le;
	//整型
	private RowDatas<Integer> in;
	//字符串
	private RowDatas<String> str;
	public LimitAdminDao() {
		re = new getRole();
		le = new getLimit();
		in = new getInt();
		str = new getStr();
	}
	
	/**
	 * 1.搜索所有的角色
	 * @return
	 */
	public List<RoleEntity> selRole(){
		String sql = "select * from tb_role";
		return queryList(sql, re);
	}
	
	/**
	 * 2.根据角色id查找权限
	 * @param role
	 * @return
	 */
	public List<LimitEntity> selLimit(int role_id){
		String sql = "select * from tb_limit where limit_id in (select limit_id from tb_role_limit where role_id="+role_id+")";
		return queryList(sql, le);
	}
	
	/**
	 * 3.根据角色姓名查角色id
	 * @param name
	 * @return
	 */
	public int selRoleId(String name){
		String sql = "select role_id from tb_role where role_name='"+name+"'";
		return query(sql, in);
	}
	
	/**
	 * 4.根据用户姓名获取角色
	 * @param name
	 * @return
	 */
	public String selNameRole(String name){
		String sql="select emp_role from tb_employee where emp_name='"+name+"'";
		return query(sql, str);
	}
	
	/**
	 * 5.根据角色id先删除该角色原有的权限
	 * @param id
	 * @return
	 */
	public int deLimit(int id){
		String sql = "delete  from tb_role_limit where role_id='"+id+"'";
		return updateExecuted(sql);
	}
	
	/**
	 * 6.重新插入该用户的权限
	 * @param limit_id
	 * @param role_id
	 * @return
	 */
	public int insLimit(int limit_id,int role_id){
		String sql = "insert into tb_role_limit values ("+limit_id+","+role_id+")";
		return updateExecuted(sql);
	}
	
	public class getRole implements RowDatas<RoleEntity>{
		@Override
		public RoleEntity getRowData(ResultSet rs) throws SQLException {
			RoleEntity re = new RoleEntity();
			re.setRolename(rs.getString("role_name"));
			re.setRoleid(rs.getInt("role_id"));
			return re;
		}
	}
	
	public class getLimit implements RowDatas<LimitEntity>{
		@Override
		public LimitEntity getRowData(ResultSet rs) throws SQLException {
			LimitEntity limit = new LimitEntity();
			limit.setLimitid(rs.getInt("limit_id"));
			limit.setLimitname(rs.getString("limit_name"));
			limit.setLimiturl(rs.getString("limit_url"));
			limit.setLimitparent(rs.getString("limit_parent_id"));
			return limit;
		}
	}
	
	public class getInt implements RowDatas<Integer>{
		@Override
		public Integer getRowData(ResultSet rs) throws SQLException {
			return rs.getInt(1);
		}
	}
	
	public class getStr implements RowDatas<String>{
		@Override
		public String getRowData(ResultSet rs) throws SQLException {
			String str=rs.getString("emp_role");
			return str;
		}
	}
}
